package com.feicheng.controller.main.admin.system;


import java.util.Map;

import javax.annotation.Resource;

import com.feicheng.persistence.entity.StoreUser;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.feicheng.common.constant.Constant;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.User;
import com.feicheng.service.IFSystemService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/main/admin/user")
@RequestMapping("/main/admin/user")
public class UserController {

    /* 通过依赖查找实现多实现BEAN的调用 */
    @Resource(name="system-service")
    Map<String, IFSystemService> systemServiceBean;

    /**
     * 根据相关入参查询user表，若无全查询user表
     * @param currentPage
     * @param pageSize
     * @param account
     * @param tel
     * @param secId
     * @return 查询到的用户和分页详情
     */
    @GetMapping("/list")
    public ResultInfo selectUser(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String account,
            String tel,
            String secId) {
        return systemServiceBean.get(Constant.MODEL_NAME.MAIN).selectUser(
                currentPage,
                pageSize,
                account,
                tel,
                secId,
                null);
    }

    /**
     * 根据相关入参查询user表是否存在相同account
     * @param account
     * @return
     */
    @GetMapping("/account/list")
    public ResultInfo selectUserByAccount(String account) {
        return systemServiceBean.get(Constant.MODEL_NAME.MAIN).selectUserByAccount(account);
    }

    /**
     * 根据用户ID查询对应用户
     * @param userId
     * @return 查询到的用户
     */
    @GetMapping("/info")
    public ResultInfo selectUserById(Integer userId) {
        return systemServiceBean.get(Constant.MODEL_NAME.MAIN).selectUserById(userId);
    }

    /**
     * 添加用户和对应角色
     * @param user
     * @param roles
     * @return
     */
    @PostMapping("/info")
    @Transactional
    public ResultInfo addUser(User user, String roles) {
        return systemServiceBean.get(Constant.MODEL_NAME.MAIN).addUser(user, roles);
    }


    /**
     * 修改用户
     * @param user
     * @param roles
     * @return
     */
    @PutMapping("/info")
    public ResultInfo updateUser(User user, String roles) {
        return systemServiceBean.get(Constant.MODEL_NAME.MAIN).updateUser(user, roles);
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/info")
    public ResultInfo deleteUser(Integer userId) {
        return systemServiceBean.get(Constant.MODEL_NAME.MAIN).deleteUser(userId);
    }

    /**
     * 修改门店用户
     * @param user
     * @param roles
     * @return
     */
    @PutMapping("/store/info")
    public ResultInfo updateStroeUser(StoreUser user, String roles) {
        return systemServiceBean.get(Constant.MODEL_NAME.SHOP).updateUser(user, roles);
    }
}
